package org.loofer.uikit;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public interface Markable {
    public static final int MARK_CENTER = 1;
    public static final int MARK_LEFT_BOTTOM = 5;
    public static final int MARK_LEFT_TOP = 2;
    public static final int MARK_RIGHT_BOTTOM = 4;
    public static final int MARK_RIGHT_TOP = 3;
    public static final int MARK_SHOW_IMAGE = 2;
    public static final int MARK_SHOW_NONE = 1;
    public static final int MARK_SHOW_NUMBER = 4;
    public static final int MARK_SHOW_TEXT = 3;

    public static class Marker implements Markable {
        private boolean mAutoPadding;
        private Context mContext;
        private int mMarkTextColor;
        private int mMarkTextSize;
        private Drawable mMarker;
        private Drawable mMarker1;
        private Drawable mMarker2;
        private Drawable mMarker3;
        private OnMarkerClickListener mMarkerClickListener;
        private Markable mMarkerClickListenerCallback;
        private int mMarkerHeight;
        private boolean mMarkerOrPositionChanged;
        private int mMarkerPaddingX;
        private int mMarkerPaddingXOffset;
        private int mMarkerPaddingY;
        private int mMarkerPaddingYOffset;
        private int mMarkerPosition;
        private boolean mMarkerPressed;
        private String mMarkerText;
        private int mMarkerType;
        private boolean mMarkerVisible;
        private boolean mMarkerVisibleWhenSelected;
        private int mMarkerWidth;
        private Paint mTextPaint;
        private View mView;

        public Marker(View view) {
            this(view, null);
        }

        public Marker(View view, AttributeSet attrs) {
            this.mMarkerVisible = false;
            this.mMarkerVisibleWhenSelected = false;
            this.mMarkerPosition = 1;
            this.mMarkerText = "";
            this.mMarkerType = 2;
            this.mMarkerWidth = -1;
            this.mMarkerHeight = -1;
            this.mAutoPadding = false;
            this.mMarkerOrPositionChanged = false;
            this.mMarkerPressed = false;
            this.mMarkTextSize = 15;
            this.mMarkTextColor = -1;
            this.mTextPaint = new Paint(1);
            this.mTextPaint.setTextAlign(Align.CENTER);
            this.mTextPaint.setColor(this.mMarkTextColor);
            this.mTextPaint.setTextSize((float) this.mMarkTextSize);
            this.mView = view;
            this.mContext = view.getContext();
            if (view instanceof Markable) {
                setMarkerClickListenerCallback((Markable) view);
            }
            TypedArray a = this.mContext.obtainStyledAttributes(attrs, R.styleable.Markable);
            Drawable marker = a.getDrawable(R.styleable.Markable_mark);
            Drawable marker1 = a.getDrawable(R.styleable.Markable_mark1);
            Drawable marker2 = a.getDrawable(R.styleable.Markable_mark2);
            Drawable marker3 = a.getDrawable(R.styleable.Markable_mark3);
            boolean markerVisible = a.getBoolean(R.styleable.Markable_markVisible, false);
            boolean markerVisibleWhenSelected = a.getBoolean(R.styleable.Markable_markVisibleWhenSelected, false);
            boolean markerAutoPadding = a.getBoolean(R.styleable.Markable_markAutoPadding, false);
            int markerWidth = a.getDimensionPixelSize(R.styleable.Markable_markWidth, -1);
            int markerHeight = a.getDimensionPixelSize(R.styleable.Markable_markHeight, -1);
            int markerOffsetX = a.getDimensionPixelOffset(R.styleable.Markable_markOffsetX, 0);
            int markerOffsetY = a.getDimensionPixelOffset(R.styleable.Markable_markOffsetY, 0);
            int markerPosition = a.getInt(R.styleable.Markable_markPosition, 1);
            int markerType = a.getInt(R.styleable.Markable_markType, 2);
            int markerTextColor = a.getColor(R.styleable.Markable_markTextColor, this.mMarkTextColor);
            int markerTextSize = a.getDimensionPixelSize(R.styleable.Markable_markTextSize, this.mMarkTextSize);
            setMarker(marker);
            setMarker1(marker1);
            setMarker2(marker2);
            setMarker3(marker3);
            setMarkerVisible(markerVisible);
            setMarkerVisibleWhenSelected(markerVisibleWhenSelected);
            setMarkerSize(markerWidth, markerHeight);
            setMarkerPaddingOffset(markerOffsetX, markerOffsetY);
            setMarkerPosition(markerPosition);
            setMarkerType(markerType);
            setMarkerTextColor(markerTextColor);
            setMarkerTextSize(markerTextSize);
            setMarkerAutoPadding(markerAutoPadding);
            a.recycle();
        }

        public void setMarkerVisible(boolean visible) {
            if (this.mMarkerVisible != visible) {
                this.mMarkerVisible = visible;
                this.mView.invalidate();
            }
        }

        public void setMarkerVisibleWhenSelected(boolean visible) {
            if (this.mMarkerVisibleWhenSelected != visible) {
                this.mMarkerVisibleWhenSelected = visible;
                this.mView.invalidate();
            }
        }

        public void setMarker(Drawable marker) {
            if (this.mMarker != marker) {
                this.mMarker = marker;
                this.mMarkerOrPositionChanged = true;
                this.mView.invalidate();
            }
        }

        public void setMarker1(Drawable marker) {
            this.mMarker1 = marker;
        }

        public void setMarker2(Drawable marker) {
            this.mMarker2 = marker;
        }

        public void setMarker3(Drawable marker) {
            this.mMarker3 = marker;
        }

        public void setMarker(int resId) {
            setMarker(this.mContext.getResources().getDrawable(resId));
        }

        public void setMarker1(int resId) {
            setMarker1(this.mContext.getResources().getDrawable(resId));
        }

        public void setMarker2(int resId) {
            setMarker2(this.mContext.getResources().getDrawable(resId));
        }

        public void setMarker3(int resId) {
            setMarker3(this.mContext.getResources().getDrawable(resId));
        }

        public void setMarkerAutoPadding(boolean enable) {
            this.mAutoPadding = enable;
        }

        public void setMarkerPosition(int position) {
            if (this.mMarkerPosition != position) {
                this.mMarkerPosition = position;
                this.mMarkerOrPositionChanged = true;
                this.mView.invalidate();
            }
        }

        public void setMarkerType(int markerType) {
            if (this.mMarkerType != markerType) {
                this.mMarkerType = markerType;
                this.mMarkerOrPositionChanged = true;
                this.mView.invalidate();
            }
        }

        public void setMarkerCount(int count) {
            if (count > 0) {
                setMarkerVisible(true);
                if (this.mMarkerType == 3 || this.mMarkerType == 4) {
                    if (count < 10) {
                        this.mMarkerText = String.valueOf(count);
                    } else if (count < 100) {
                        this.mMarkerText = String.valueOf(count);
                    } else {
                        this.mMarkerText = "99+";
                    }
                    setActiveMarkerByText();
                }
            } else {
                setMarkerVisible(false);
            }
            this.mMarkerOrPositionChanged = true;
            this.mView.invalidate();
        }

        public void setMarkerText(String text) {
            if (this.mMarkerText == null || !this.mMarkerText.equals(text)) {
                this.mMarkerText = text;
                setActiveMarkerByText();
                this.mMarkerOrPositionChanged = true;
                this.mView.invalidate();
            }
        }

        public void setMarkerTextSize(int textSize) {
            if (this.mMarkTextSize != textSize) {
                this.mMarkTextSize = textSize;
                this.mTextPaint.setTextSize((float) this.mMarkTextSize);
                this.mMarkerOrPositionChanged = true;
                this.mView.invalidate();
            }
        }

        public void setMarkerTextColor(int textColor) {
            if (this.mMarkTextColor != textColor) {
                this.mMarkTextColor = textColor;
                this.mTextPaint.setColor(this.mMarkTextColor);
                this.mMarkerOrPositionChanged = true;
                this.mView.invalidate();
            }
        }

        public void setMarkerPaddingOffset(int xOffset, int yOffset) {
            if (this.mMarkerPaddingXOffset != xOffset || this.mMarkerPaddingYOffset != yOffset) {
                this.mMarkerPaddingXOffset = xOffset;
                this.mMarkerPaddingYOffset = yOffset;
                this.mView.invalidate();
            }
        }

        public void setMarkerSize(int width, int height) {
            if (this.mMarkerWidth != width || this.mMarkerHeight != height) {
                this.mMarkerWidth = width;
                this.mMarkerHeight = height;
                this.mMarkerOrPositionChanged = true;
                this.mView.invalidate();
            }
        }

        public boolean isMarkerVisible() {
            return this.mMarkerVisible || (this.mMarkerVisibleWhenSelected && this.mView.isSelected());
        }

        public void setOnMarkerClickListener(OnMarkerClickListener listener) {
            this.mMarkerClickListener = listener;
        }

        protected void setMarkerClickListenerCallback(Markable callback) {
            this.mMarkerClickListenerCallback = callback;
        }

        public boolean onTouchEvent(MotionEvent event) {
            int action = event.getAction();
            if (isMarkerTouched(event)) {
                if (action == 0) {
                    this.mMarkerPressed = true;
                    return true;
                } else if (action == 1 && this.mMarkerPressed) {
                    notifyMarkerClick();
                    this.mMarkerPressed = false;
                    return true;
                }
            }
            if (action == 1 || action == 3) {
                this.mMarkerPressed = false;
            }
            return false;
        }

        public void draw(Canvas canvas) {
            if (this.mMarkerOrPositionChanged) {
                updateMarkerLayout();
                this.mMarkerOrPositionChanged = false;
            }
            if (isMarkerAvailable()) {
                int paddingX = getCurrentMarkerPaddingX();
                int paddingY = getCurrentMarkerPaddingY();
                boolean isTextMode = isTextMode();
                int textLength = 0;
                Rect textBoundRect = null;
                if (isTextMode) {
                    textLength = this.mMarkerText.length();
                    textBoundRect = new Rect();
                    this.mTextPaint.getTextBounds(this.mMarkerText, 0, textLength, textBoundRect);
                    if (textLength > 1 && this.mAutoPadding) {
                        canvas.translate((float) ((textBoundRect.width() / textLength) / 2), 0.0f);
                    }
                }
                canvas.save();
                if (!(paddingX == 0 && paddingY == 0)) {
                    canvas.translate((float) paddingX, (float) paddingY);
                }
                this.mMarker.draw(canvas);
                canvas.restore();
                if (isTextMode && textLength > 0) {
                    canvas.drawText(this.mMarkerText, (float) ((getMarkerWidth() / 2) + paddingX), (float) (((getMarkerHeight() + textBoundRect.height()) / 2) + paddingY), this.mTextPaint);
                }
            }
        }

        public void updateMarkerLayout() {
            if (this.mMarker != null) {
                int viewWidth = this.mView.getWidth();
                int viewHeight = this.mView.getHeight();
                int width = getMarkerWidth();
                int height = getMarkerHeight();
                this.mMarkerPaddingX = getMarkerPaddingX(viewWidth, width, this.mMarkerPosition);
                this.mMarkerPaddingY = getMarkerPaddingY(viewHeight, height, this.mMarkerPosition);
                this.mMarker.setBounds(0, 0, width, height);
            }
        }

        private boolean isTextMode() {
            return this.mMarkerType == 4 || this.mMarkerType == 3;
        }

        private void setActiveMarkerByText() {
            int length = this.mMarkerText.length();
            if (length == 1) {
                setMarker(this.mMarker1);
            } else if (length == 2) {
                setMarker(this.mMarker2);
            } else {
                setMarker(this.mMarker3);
            }
        }

        private int getMarkerWidth() {
            return this.mMarkerWidth > 0 ? this.mMarkerWidth : this.mMarker.getIntrinsicWidth();
        }

        private int getMarkerHeight() {
            return this.mMarkerHeight > 0 ? this.mMarkerHeight : this.mMarker.getIntrinsicHeight();
        }

        private int getCurrentMarkerPaddingX() {
            return this.mMarkerPaddingX + this.mMarkerPaddingXOffset;
        }

        private int getCurrentMarkerPaddingY() {
            return this.mMarkerPaddingY + this.mMarkerPaddingYOffset;
        }

        private static int getMarkerPaddingX(int width, int markerWidth, int position) {
            switch (position) {
                case 2:
                    return 0;
                case 3:
                    return width - markerWidth;
                case 4:
                    return width - markerWidth;
                case 5:
                    return 0;
                default:
                    return (width - markerWidth) / 2;
            }
        }

        private static int getMarkerPaddingY(int height, int markerHeight, int position) {
            switch (position) {
                case 2:
                    return 0;
                case 3:
                    return 0;
                case 4:
                    return height - markerHeight;
                case 5:
                    return height - markerHeight;
                default:
                    return (height - markerHeight) / 2;
            }
        }

        private void notifyMarkerClick() {
            if (this.mMarkerClickListener != null) {
                Markable callback;
                if (this.mMarkerClickListenerCallback != null) {
                    callback = this.mMarkerClickListenerCallback;
                } else {
                    Object callback2 = this;
                }
                this.mMarkerClickListener.onClick(callback);
            }
        }

        private boolean isMarkerAvailable() {
            return (this.mMarkerType == 1 || !isMarkerVisible() || this.mMarker == null) ? false : true;
        }

        private boolean isMarkerTouched(MotionEvent event) {
            if (!isMarkerAvailable() || this.mMarkerClickListener == null) {
                return false;
            }
            int x = (int) event.getX();
            int y = (int) event.getY();
            int lowerX = getCurrentMarkerPaddingX();
            int upperX = lowerX + getMarkerWidth();
            int lowerY = getCurrentMarkerPaddingY();
            int upperY = lowerY + getMarkerHeight();
            if (x < lowerX || x > upperX || y < lowerY || y > upperY) {
                return false;
            }
            return true;
        }

        public void getMarkerSize(int[] size) {
            if (size == null || size.length < 2) {
                throw new IllegalArgumentException("location must be an array of two integers");
            }
            int width = 0;
            int height = 0;
            if (this.mMarker != null) {
                width = getMarkerWidth();
                height = getMarkerHeight();
            }
            size[0] = width;
            size[1] = height;
        }
    }

    public interface OnMarkerClickListener {
        void onClick(Markable markable);
    }

    void getMarkerSize(int[] iArr);

    boolean isMarkerVisible();

    void setMarker(int i);

    void setMarker(Drawable drawable);

    void setMarker1(int i);

    void setMarker1(Drawable drawable);

    void setMarker2(int i);

    void setMarker2(Drawable drawable);

    void setMarker3(int i);

    void setMarker3(Drawable drawable);

    void setMarkerAutoPadding(boolean z);

    void setMarkerCount(int i);

    void setMarkerPaddingOffset(int i, int i2);

    void setMarkerPosition(int i);

    void setMarkerSize(int i, int i2);

    void setMarkerText(String str);

    void setMarkerTextColor(int i);

    void setMarkerTextSize(int i);

    void setMarkerType(int i);

    void setMarkerVisible(boolean z);

    void setMarkerVisibleWhenSelected(boolean z);

    void setOnMarkerClickListener(OnMarkerClickListener onMarkerClickListener);
}
